(function(){
	var AjaxCrud = QualityCheck.AjaxCRUD;
	var serverUrl = QualityCheck.GPUD.ServiceConfig;
	$(function(){
		var usercode = $("#user-name",parent.parent.document).attr("code");
		var userkey = $("#user-name",parent.parent.document).attr("key");
		var usertype = $("#user-name",parent.parent.document).attr("usertype");
		keyCheck(usercode,usertype,userkey,function(){
			initSmPages();
			window.onresize = function () {
				initSmPages();
			};
			pagebtns();
		});
	});
	function pagebtns(){
		$("#regon-save-btns").click(function(){
			if(checkRegon() && check2("check-fail")){
				addDepartment();
			}
		});	
		$("#regon-departmentcode,#regon-department,#regon-office").unbind("keyup");
		$("#regon-departmentcode,#regon-department,#regon-office").keyup(function(){
			this.value=this.value.replace(/^ +| +$/g,'');
		});
		check("regon-departmentcode",isNull,"null");
		check("regon-department",isNull,"null");
		check("regon-office",isNull,"null");
		
	}
	function addDepartment(){
		var userKey = $("#user-name",parent.parent.document).attr("key");
		var schoolcode = $("#user-name",parent.parent.document).attr("schoolcode");
		var params = {};
		var param = {};
		params["departmentcode"] = encodeURI($("#regon-departmentcode").val());			
		params["department"] = encodeURI($("#regon-department").val());
		params["office"] = encodeURI($("#regon-office").val());
		params["schoolcode"] = schoolcode;
		for(var key in params){
			if(params[key] != ""){
				param[key] = params[key];
			}
		}
		var data = {
				cmd:"addDepartment",
				model:"department",
				userKey:userKey,
				data:param,
				remark:"",						
		};
		processBegin("正在保存中···");
		AjaxCrud.get(serverUrl.ServerLoginUrl,data,function(res){
			if(res.result == 1 && res.data != undefined && res.data != ""){
				processEnd("添加部门成功");
			}else{
				processError("添加失败,编码重复");
			}
		},null,null);
	}
	function checkRegon(){
		if($("#regon-departmentcode").val() == ""){
			$("#regon-departmentcode-ex").html("不能为空");
			return false;   
		}else if($("#regon-department").val() == ""){
			$("#regon-department-ex").html("不能为空");
			return false;  
		}else if($("#regon-office").val() == ""){
			$("#regon-office-ex").html("不能为空");
			return false;  
		}else{
			return true;
		}
	}
	function initSmPages(){
		var rcH = $(".body-right",parent.document).height() - $(".right-title").height()-3;
		$(".right-content").height(rcH);
		
		var cdsH = $(".right-content").height() - $(".content-title").height() - 40;
		$(".content-data-show").height(cdsH);
	}
	
})();